<?php

namespace Es\Struct\Fields;

use Es\Constant\FieldsConst;
use Es\Contract\FieldsInterface;
use Es\Exceptions\CommonExceptions;

/**
 * 索引结构体
 */
class IntStruct implements FieldsInterface
{
    
    private $name;
    private $type;
    
    const TYPE_LIST = [
        FieldsConst::BYTE        => 1,
        FieldsConst::SHORT       => 1,
        FieldsConst::INT         => 1,
        FieldsConst::LONG        => 1 ,
        FieldsConst::UNSIGN_LONG => 1,
    ];

    /**
     * @throws CommonExceptions
     */
    public function __construct(string $name, string $type = FieldsConst::INT){
        $this->name = $name;
        $this->type = $type;
        if(!isset(self::TYPE_LIST[$type])){
            throw new CommonExceptions(CommonExceptions::FILEDS_NUM_NOT_FOUNT);
        }
    }   

    public function asArray(): array
    {
        return [ 
            $this->name, 
            [
                'type'  => $this->type
            ]
        ];
    }

}
